package com.shujia.scala

import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}

object Demo8Jdbc {
  def main(args: Array[String]): Unit = {


    //1、加载驱动
    Class.forName("com.mysql.jdbc.Driver")

    //2、建立链接
    val con: Connection = DriverManager.getConnection("jdbc:mysql://192.168.129.201:3306/bigdata", "root", "123456")

    //3、写sql查数据
    val stat: PreparedStatement = con.prepareStatement("select * from students")

    //4、执行查询
    val resultSet: ResultSet = stat.executeQuery()

    //5、解析数据
    while (resultSet.next()) {
      //通过列名取数据
      val id: Long = resultSet.getLong("id")
      val name: String = resultSet.getString("name")
      val age: Long = resultSet.getLong("age")
      val gender: String = resultSet.getString("gender")
      val clazz: String = resultSet.getString("clazz")

      println(id + "\t" + name + "\t" + age + "\t" + gender + "\t" + clazz)
    }

    //关闭链接
    stat.close()
    con.close()


  }

}
